package com.oneraku.service.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneraku.service.dao.AgentSeckillMapper;
import com.oneraku.service.domain.AgentSeckill;
import com.oneraku.service.domain.AgentStepInfo;
import com.oneraku.service.exceptions.InfoException;
import com.oneraku.service.service.IAgentSeckillService;
import com.oneraku.service.service.IAgentStepInfoService;
import com.oneraku.service.utils.AuthBean;
import com.oneraku.service.utils.R;
import com.oneraku.service.utils.RequestUserUtils;
import com.oneraku.service.utils.StepEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 代拍 服务实现类
 * </p>
 *
 * @author renhui <337375464@qq.com>
 * @since 2020-02-25
 */
@Service
public class AgentSeckillServiceImpl extends ServiceImpl<AgentSeckillMapper, AgentSeckill> implements IAgentSeckillService {
    @Autowired
    private IAgentStepInfoService stepInfoService;
    private void checkBean(AgentSeckill agentSeckill) {
        switch (agentSeckill.getType().intValue()) {
            case 0:
                if (StringUtils.isEmpty(agentSeckill.getUrl()) && agentSeckill.getUrl().indexOf("http") != 0) {
                    throw new InfoException("代拍链接不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getName())) {
                    throw new InfoException("代拍名称不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getPriceRefer())){
                throw new InfoException("代拍预估不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getPriceCeil())) {
                    throw new InfoException("代拍上限不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getNum())) {
                    throw new InfoException("代拍数量不能为空");
                }
                try{
                    int num = Integer.valueOf(agentSeckill.getNum());
                    if(num <= 0){
                        throw new InfoException("代拍数量至少一件");
                    }
                }catch (Exception e){
                    throw new InfoException("代拍数量必须是数字");
                }
                break;
            case 1:
                if (StringUtils.isEmpty(agentSeckill.getUrl()) && agentSeckill.getUrl().indexOf("http") != 0) {
                    throw new InfoException("代购链接不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getName())) {
                    throw new InfoException("代购商品名称不能为空");
                }
                if (StringUtils.isEmpty(agentSeckill.getNum())) {
                    throw new InfoException("代购数量不能为空");
                }
                try{
                    int num = Integer.valueOf(agentSeckill.getNum());
                    if(num <= 0){
                        throw new InfoException("代购数量至少一件");
                    }
                }catch (Exception e){
                    throw new InfoException("代购数量必须是数字");
                }
                break;
            case 2:
                break;
            default:
                throw new InfoException("类型未知");
        }

    }

    @Override
    public R listAgentSeckill(AgentSeckill agentSeckill) {
        AuthBean authBean = RequestUserUtils.getUser();
        return R.okPage(page(agentSeckill.page(), Wrappers.<AgentSeckill>lambdaQuery().eq(AgentSeckill::getCreator, authBean.getId())));
    }

    @Override
    public R editAgentSeckill(AgentSeckill agentSeckill) {
        String agentBuyId = agentSeckill.getId();
        if (StringUtils.isEmpty(agentBuyId)) {
            throw new InfoException("更新标识未知");
        }
        checkBean(agentSeckill);
        AuthBean authBean = RequestUserUtils.getUser();
        agentSeckill.setCreator(authBean.getId());
        return R.ok(save(agentSeckill));
    }

    @Override
    public R delAgentSeckill(AgentSeckill agentSeckill) {
        String agentBuyId = agentSeckill.getId();
        if (StringUtils.isEmpty(agentBuyId)) {
            throw new InfoException("删除标识未知");
        }
        AuthBean authBean = RequestUserUtils.getUser();
        AgentSeckill localAgentBuy = getById(agentBuyId);
        if (null == localAgentBuy) {
            throw new InfoException("未找到相关的代购订单");
        }
        if (!localAgentBuy.getCreator().equals(authBean.getId())) {
            throw new InfoException("没有权限删除该代购订单");
        }
        return R.ok(removeById(localAgentBuy.getId()));
    }

    @Override
    public R addAgentSeckill(AgentSeckill agentSeckill) {
        checkBean(agentSeckill);
        agentSeckill.setId(null);
        Integer type = agentSeckill.getType();
        if (type == null) {
            throw new InfoException("代拍代购类型未知");
        }
        if(StringUtils.isEmpty(agentSeckill.getUrl()) || !agentSeckill.getUrl().startsWith("http")){
            throw new InfoException("请输入正确的购买链接!");
        }

        if (type.intValue() == 0) {
            agentSeckill.setStatus(0);
            agentSeckill.setStep("0");
        } else if (type.intValue() == 1) {
            agentSeckill.setStatus(10);
            agentSeckill.setStep("10");
        } else if (type.intValue() == 2) {
            agentSeckill.setStatus(20);
            agentSeckill.setStep("20");
        } else {
            throw new InfoException("代拍代购类型未知");
        }


        AuthBean authBean = RequestUserUtils.getUser();
        agentSeckill.setCreator(authBean.getId());
        save(agentSeckill);


        List<AgentStepInfo> stepInfoList = new ArrayList<AgentStepInfo>();
        if (type.intValue() == 0) {
            stepInfoList.addAll(StepEnum.BuyStepEnum.getStepList(agentSeckill.getId()));
        } else if (type.intValue() == 1) {
            stepInfoList.addAll(StepEnum.SkillStepEnum.getStepList(agentSeckill.getId()));
        } else if (type.intValue() == 2) {
            stepInfoList.addAll(StepEnum.TransferStepEnum.getStepList(agentSeckill.getId()));
        } else {
            throw new InfoException("代拍代购类型未知");
        }
        stepInfoService.saveBatch(stepInfoList);
        return R.ok(agentSeckill.getId());
    }
}
